Imports System.IO

Public Class frmMain


    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Dim MyResult As DialogResult
        MyResult = openFile.ShowDialog
        If MyResult = Windows.Forms.DialogResult.OK Then
            txtFileToAdd.Text = openFile.FileName
        End If
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim MyFile As FileInfo
        Try
            MyFile = New FileInfo(txtFileToAdd.Text)
        Catch ex As Exception
            MsgBox("Not a valid file! Is the line blank or malformed?")
            Return
        End Try

        If MyFile.Exists = False Then
            MsgBox("No such file exists!")
            Return
        End If

        'otherwise
        Dim arrString(2) As String
        arrString(0) = txtFileToAdd.Text.Split("\")(txtFileToAdd.Text.Split("\").GetUpperBound(0))
        arrString(1) = txtFileToAdd.Text
        arrString(2) = MyFile.Length.ToString()
        Dim li As New ListViewItem(arrString)
        lstFilesToPrint.Items.Add(li)
    End Sub

    Private Sub btnExecute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExecute.Click
        Me.Cursor = Cursors.WaitCursor
        Dim objFiles As New ArrayList()
        Dim li As ListViewItem
        For Each li In lstFilesToPrint.Items
            objFiles.Add(li.SubItems(1).Text)
            Debug.Print(li.SubItems(1).Text)
        Next
        Dim arrFiles() As String = objFiles.ToArray(System.Type.GetType("System.String"))
        DoPrintList(arrFiles)
        lstFilesToPrint.Items.Clear()
        Me.Cursor = Cursors.Default
    End Sub

    Private Function GetExtension(ByVal FileName As String)
        Dim objFile As New FileInfo(FileName)
        Return objFile.Extension.ToLower()
    End Function


    Sub DoPrintList(ByVal arrFiles() As String)
        Dim FilePath As String
        For Each FilePath In arrFiles
            Select Case GetExtension(FilePath)
                Case ".pdf"
                    Try
                        Dim AVApp As Object 'Acrobat.CAcroApp
                        Dim AVDoc As Object 'Acrobat.CAcroAVDoc
                        Dim PDDoc As Object 'Acrobat.CAcroPDDoc
                        AVApp = CreateObject("AcroExch.App")
                        PDDoc = CreateObject("AcroExch.PDDoc")
                        AVDoc = CreateObject("AcroExch.AVDoc")
                        Dim iPages As Integer
                        PDDoc.Open(FilePath)
                        iPages = PDDoc.GetNumPages()
                        AVDoc = PDDoc.OpenAVDoc(FilePath)
                        AVDoc.PrintPages(0, iPages - 1, 2, True, False)
                        AVDoc.Close(True)
                        PDDoc.Close()
                        clsLogData.AddRunRecord(FilePath, "sent", iPages)
                    Catch E As Exception
                        MsgBox("Print job failed: " & E.ToString())
                        clsLogData.AddRunRecord(FilePath, "failed")
                    End Try
                Case Else
                    PrintOfficeDoc(FilePath)
            End Select
        Next
    End Sub

    Private Sub PrintOfficeDoc(ByVal FileName As String)
        Dim FileOb As Object, iCount As Integer
        FileOb = GetObject(FileName)
        Dim FileSysOb As New FileInfo(FileName)

        If FileSysOb.Extension.ToLower() = ".doc" Then
            FileOb.Activate()
            FileOb.PrintOut()
            ''Word must keep running for print jobs
            Do Until FileOb.Parent.BackgroundPrintingStatus = 0
                System.Windows.Forms.Application.DoEvents()
            Loop
            FileOb.Close()
        ElseIf FileSysOb.Extension.ToLower() = ".rtf" Then
            FileOb.Activate()
            FileOb.PrintOut()
            ''Word must keep running for print jobs
            Do Until FileOb.Parent.BackgroundPrintingStatus = 0
                System.Windows.Forms.Application.DoEvents()
            Loop
            FileOb.Close()
        ElseIf FileSysOb.Extension.ToLower() = ".xls" Then
            'loop through excels work book work sheet collection
            For iCount = 1 To FileOb.Worksheets.Count
                FileOb.Worksheets(iCount).PrintOut()
            Next iCount
            FileOb.Close()
            ''do printing for other file types here
        ElseIf FileSysOb.Extension.ToLower() = ".ppt" Then
            FileOb.PrintOut()
            FileOb.Close()
        End If

        clsLogData.AddRunRecord(FileName, "sent")
    End Sub

    Private Sub lstFilesToPrint_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstFilesToPrint.DragEnter
        ' Make sure that the format is text.
        If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
            ' Allow drop.
            e.Effect = DragDropEffects.Copy
        Else
            ' Do not allow drop.
            e.Effect = DragDropEffects.None
        End If
    End Sub

    Private Sub lstFilesToPrint_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lstFilesToPrint.DragDrop
        Me.Cursor = Cursors.WaitCursor
        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
            ' Assign the file names to a string array, in 
            ' case the user has selected multiple files.
            Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
            Dim strFile As String
            For Each strFile In files
                AddDroppedFile(strFile)
            Next

        End If

        Me.Cursor = Cursors.Default
    End Sub

    Private Sub AddDroppedFile(ByVal FileName As String)
        Dim MyFile As New FileInfo(FileName)

        If MyFile.Exists = False Then
            MsgBox("No such file exists!")
            Return
        End If

        If MyFile.Extension.ToLower() <> ".pdf" And _
        MyFile.Extension.ToLower() <> ".doc" And _
        MyFile.Extension.ToLower() <> ".xls" And _
        MyFile.Extension.ToLower() <> ".rtf" And _
        MyFile.Extension.ToLower() <> ".ppt" _
    Then Return

        'otherwise
        Dim arrString(2) As String
        arrString(0) = FileName.Split("\")(FileName.Split("\").GetUpperBound(0))
        arrString(1) = FileName
        arrString(2) = MyFile.Length.ToString()
        Dim li As New ListViewItem(arrString)
        lstFilesToPrint.Items.Add(li)
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Dim MyResult As DialogResult
        MyResult = openFile.ShowDialog
        If MyResult = Windows.Forms.DialogResult.OK Then
            txtFileToAdd.Text = openFile.FileName
        End If
    End Sub

    Private Sub ClearListToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearListToolStripMenuItem.Click
        Dim MyResponse As DialogResult = Windows.Forms.MessageBox.Show("Clear file list?", "File List", MessageBoxButtons.OKCancel)
        If MyResponse <> Windows.Forms.DialogResult.OK Then Return
        lstFilesToPrint.Items.Clear()
    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Dim MyResponse As DialogResult = Windows.Forms.MessageBox.Show("Exit and stop all work?", "Batch Printer", MessageBoxButtons.OKCancel)
        If MyResponse <> Windows.Forms.DialogResult.OK Then Return
        Application.Exit()
    End Sub

    Private Sub AddFolderToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFolderToolStripMenuItem.Click
        Me.Cursor = Cursors.WaitCursor
        Dim dlgr As DialogResult = openFolder.ShowDialog
        If dlgr <> Windows.Forms.DialogResult.OK Then Return
        Dim FolderPath As String = openFolder.SelectedPath
        Dim objFolder As New DirectoryInfo(FolderPath)
        Dim objFile As FileInfo
        For Each objFile In objFolder.GetFiles()
            AddDroppedFile(objFile.FullName)
        Next
        Me.Cursor = Cursors.Default
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

    End Sub

    Private Sub LogToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogToolStripMenuItem.Click
        frmLogViewer.ShowDialog()
    End Sub
End Class
